package q141

import (
	"strconv"
	"strings"
)

type ListNode struct {
	Val  int
	Next *ListNode
}

//创建单个listNode
func MakeListNode(val int, next *ListNode) *ListNode {
	return &ListNode{
		Val:  val,
		Next: next,
	}
}

//创建多个listNodes
func MakeListNodes(vals ...int) *ListNode {
	var head *ListNode
	for i := len(vals) - 1; i >= 0; i-- {
		head = MakeListNode(vals[i], head)
	}
	return head
}

//比较是否一样
func (node *ListNode) Compare(compNode *ListNode) bool {
	for node != nil {
		if node.Val != compNode.Val {
			return false
		}
		node = node.Next
		compNode = compNode.Next
	}

	if compNode != nil {
		return false
	}

	return true
}

//输出其字符串
func (node *ListNode) String(dep string) string {
	arr := []string{}
	cycle:=map[*ListNode]bool{}
	for node != nil {
		//环检测
		if _,ok:=cycle[node];ok{
			break
		}
		cycle[node]=true

		arr = append(arr, strconv.Itoa(node.Val))
		node = node.Next
	}

	return strings.Join(arr, dep)
}
